package gameMiniGames;
/**
 * 
 */
//import gameInterface.MiniGames;
import gameAudio.Audio;

import java.awt.*;
import javax.swing.*;

import java.awt.Container;
import java.awt.event.*;
import java.io.File;
/**
 * @author Logical Solutions
 *
 */
/*public class ConnectFour { // implements MiniGames{

	private static final int width = 640; //650
	private static final int height = 480; //500	
	
	public static void main(String args[])
	{
		SwingUtilities.invokeLater(new Runnable()
		{
			public void run()
			{
				createAndShowGUI();
			}
		});
	}
	public static void createAndShowGUI()
	{	
		//Board board = new Board();
		GameFrame frame = new GameFrame(width,height); //WIDTH,HEIGHT
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.setVisible(true);
		frame.setResizable(false);
	}
	
	/*@Override
	public void endGame() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public String showDescription(String description) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public String showHint(String hint) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public int showTime(int timer) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public void startGame() {
		// TODO Auto-generated method stub
		
	}

}*/

class Board
{
	private int[][] board;
	private int currentPlayer; //1-player1 and 2-player2
	private int c4;
	private int playerPos;
	int roomValue = 102;
	//File file = new File("src/gameMiniGames/gameMusic1.mid");
	//Audio aud = new Audio();
	
	public Board()
	{
		//aud.playMusic(file, roomValue);
		board = new int[7][6];
		for(int row=0;row<6;row++)
		{
			for(int col=0;col<7;col++)
			{
				board[col][row]=0;
			}
		}
		currentPlayer =1;
		c4=0;
	}
	
	public void clearBoard()
	{
		for(int row=0;row<6;row++)
		{
			for(int col=0;col<7;col++)
			{
				board[col][row]=0;
			}
		}
	}
	
	public int getPlayerValue(int col,int row)
	{
		if(col<0 || col>6 || row<0 || row>5)
		{
			return -1;
		}
		return board[col][row];
	}
	
	public boolean isValidMove(int col)
	{
		if(col>=0 && col<6 && board[col][5]==0)
		{
			return true;
		}
		return false;
	}
	
	public int dropPiece(int col)
	{
		int openRow=-1;
		System.out.println(isColFull(col));
		if(isColFull(col))
		{
			JOptionPane.showMessageDialog(new JPanel(),"Cannot drop piece here!");
			return -1;
		}
		
		else
		{
			/*for(int row=0;row<6;row++)
			{
				if(board[col][row]==0)
				{
					openRow=row;
				}
				else if(board[col][row]!=0)
				{
					break;
				}
			}*/
			
			openRow=5;
			for(;openRow>=0&&board[col][openRow]!=0;openRow--);
			
			System.out.println("row: "+openRow);
	
			//JOptionPane.showMessageDialog(new JPanel(),"Cannot drop piece here");
			
			board[col][openRow]=currentPlayer;//place player# in cell
			currentPlayer=(currentPlayer%2)+1;
		}		
		return openRow;//return open row spot
	}
	public boolean isColFull(int col)
	{
		for(int row=0;row<6;row++)
		{
			if(board[col][row]==0)
			{
				return false;
			}
			else if(board[col][row]!=0)
			{
				break;
			}
		}
		return true;
	}
	public int findEmptyCol()
	{
		int col=0;
		for(int row=0;row<6;row++)
		{
			for(; col<7; col++)
			{
				if(board[col][row]==0)
				{
					return col;
				}
				else if(board[col][row]!=0)
				{
					System.out.println("keep going");
				}
			}
		}
		return col;
	}
	public boolean horizontal(int col,int row)
	{
		if(col>3)
		{
			return false;
		}
		playerPos = board[col][row];
		c4=1;
		for(int i=(col+1);i<(col+4);i++)
		{
			if(board[i][row]==playerPos)
			{
				c4++;
			}
			else
			{
				break;
			}
		}
		return (c4==4);
	}
	public boolean vertical(int col,int row)
	{
		if(row<2)
		{
			return false;
		}
		playerPos = board[col][row];
		c4=1;		
		for(int i=1;i<4;i++)
		{
			if(board[col][row-i]==playerPos)
			{
				c4++;
			}
			else
			{
				break;
			}
		}
		return (c4==4);
	}
	public boolean diagonal1(int col,int row)
	{
		if(row<2 || col<3)
		{
			return false;
		}
		// diagonal /
		playerPos = board[col][row];
		c4=1;
		for(int i=1;i<4;i++)
		{
			if(board[col-i][row-i]==playerPos)
			{
				c4++;
			}
			else
			{
				break;
			}
		}
		return (c4==4);
	}
	public boolean diagonal2(int col,int row)
	{ 
		if(col>3 || row<2)
		{
			return false;
		}
		// diagonal \
		playerPos = board[col][row];
		c4=1;
		for(int i=1;i<4;i++)
		{
			if(board[col+i][row-i]==playerPos)
			{
				c4++;
			}
			else
			{
				break;
			}
		}
		return (c4==4);
	}
	public boolean hasWon()
	{
		if(getWinner()!=-1)
		{
			return true;
		}
		return false;		
	}
	public int getWinner()
	{
		int winner = -1;
		for(int row=0;row<6;row++)
		{
			for(int col=0;col<7;col++)
			{
				if(board[col][row]!=0 && (vertical(col,row) || horizontal(col,row) || diagonal1(col,row) || diagonal2(col,row)))
				{
					winner = board[col][row];
				}
			}
		}
		return winner;
	}
}

public class ConnectFour extends JPanel implements MouseListener
{	
	private Board board;
	private JLabel[][] pos;
	private int currentPlayer;
	private int winner;

	final int WIDTH = 640;
	final int HEIGHT = 480;

	//private Container container;
	//private JPanel controlPanel = new JPanel();
	//private JPanel statsPanel = new JPanel();
	//private JPanel gamePanel = new JPanel();
	//private JPanel piecePanel = new JPanel();
	//private JPanel buttonPanel = new JPanel();
	//private JPanel infoPanel = new JPanel();
	
	/*private JLabel piece = new JLabel();

	private JLabel p1 = new JLabel("Player 1: Red");
	private JLabel p2 = new JLabel("Player 2 (AI): Black");
	private JLabel redMoves = new JLabel("Number of Moves: 0");
	private JLabel blackMoves = new JLabel("Number of Moves: 0");
	private JLabel space = new JLabel(" ");*/

	private JButton newgame = new JButton("New Game");
	private JButton pause = new JButton("Pause");
	private boolean isRunning = false;
	private boolean isPaused = true;

	public boolean played = false;
	//private Timer timer;
	
	public ConnectFour()
	{
		//this.WIDTH = width;
		//this.HEIGHT = height;
		board = new Board();
		//timer=new Timer();
		
		//setTitle("Connect Four");
		//setSize(width,height);
		setBounds(0,0,640,480);
		
		//Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
        //setLocation(screenSize.width/2-getWidth()/2,screenSize.height/2-getHeight()/2);
		
		//container = getContentPane();
		//container.setLayout(new BorderLayout());
		
		setLayout(new GridLayout(6,7));
		setBackground(Color.yellow);
		setAlignmentX(Component.CENTER_ALIGNMENT);
        setAlignmentY(Component.CENTER_ALIGNMENT);
        setBorder(BorderFactory.createCompoundBorder(	BorderFactory.createLoweredBevelBorder(),
                												BorderFactory.createEmptyBorder(5,5,5,5)));
		pos = new JLabel[7][6];
		
		currentPlayer = 1;
		
		for(int row=0;row<6;row++)
		{
			for(int col=0;col<7;col++)
			{
				pos[col][row] = new JLabel();
				//pos[col][row].setHorizontalAlignment(SwingConstants.CENTER);
				pos[col][row].setIcon(new ImageIcon("src/gameMiniGames/ConnectFourImages/gray.png"));
				add(pos[col][row]);		
				//add(pos[col][row], new Integer(0));
			}
		}		
		
		//controlPanel.setBackground(Color.white);
		//statsPanel.setBackground(Color.white);
		//piecePanel.setBackground(Color.white);
		//buttonPanel.setBackground(Color.white);
		//infoPanel.setBackground(Color.white);

		//controlPanel.setLayout(new BorderLayout());
		//statsPanel.setLayout(new BoxLayout(statsPanel, BoxLayout.PAGE_AXIS));
		//piecePanel.setLayout(new BorderLayout());
		//buttonPanel.setLayout(new BorderLayout());
		//infoPanel.setLayout(new GridLayout(3,1));

		//statsPanel.setBorder(BorderFactory.createCompoundBorder(BorderFactory.createTitledBorder("Player Information"),
		//														BorderFactory.createEmptyBorder(5, 5, 5, 5)));
		//piecePanel.setBorder(BorderFactory.createCompoundBorder(BorderFactory.createTitledBorder("Current Turn"),
		//														BorderFactory.createEmptyBorder(5, 5, 5, 5)));
		
		//piece.setHorizontalAlignment(SwingConstants.CENTER);
		//piece.setIcon(new ImageIcon("src/gameMiniGames/ConnectFour/gray.png"));
		//piecePanel.add(piece, BorderLayout.CENTER);
		
		//newgame.addActionListener(this);
		//pause.addActionListener(this);

		//statsPanel.add(p1);
		//statsPanel.add(redMoves);
		//statsPanel.add(space);
		//statsPanel.add(p2);			
		//statsPanel.add(blackMoves);

		//infoPanel.add(piecePanel);
		//infoPanel.add(statsPanel);

		//buttonPanel.add(newgame, BorderLayout.NORTH);//play
		//buttonPanel.add(pause, BorderLayout.CENTER);//quit			
		
		//add(newgame);
		//add(pause);
		
		isRunning=true;
		isPaused=false;
		
		restartGame();	

		//controlPanel.add(infoPanel, BorderLayout.CENTER);
		//controlPanel.add(buttonPanel, BorderLayout.SOUTH);

		//container.add(gamePanel,BorderLayout.CENTER);
		//container.add(controlPanel,BorderLayout.EAST);
		//add(gamePanel, new Integer(0));
		//add(buttonPanel, new Integer(1));
		//add(controlPanel, new Integer(1));
	}
	
	public boolean beenPlayed()
	{
		return played;
	}
	
	public void setPlayed(boolean p)
	{
		played = p;
	}
	
	public int getCol(JLabel label)
	{
		int currentCol = -1;
		for(int row=0;row<6;row++)
		{
			for(int col=0;col<7;col++)
			{
				if(pos[col][row]==label)
				{
					currentCol = col;
				}
			}
		}
		return currentCol;
	}
	private int redCount=1;
	private int blackCount=1;
	public void drawPiece(int col,int row)
	{
		if(currentPlayer == 1)
		{
			pos[col][row].setIcon(new ImageIcon("src/gameMiniGames/ConnectFourImages/red.png"));
			//piece.setIcon(new ImageIcon("src/gameMiniGames/ConnectFour/black.png"));
			//redMoves.setText("Number of Moves: "+redCount++);
			redCount++;
		}
		else if(currentPlayer == 2)
		{
			pos[col][row].setIcon(new ImageIcon("src/gameMiniGames/ConnectFourImages/black.png"));
			//piece.setIcon(new ImageIcon("src/gameMiniGames/ConnectFour/red.png"));
			//blackMoves.setText("Number of Moves: "+blackCount++);
			blackCount++;
		}
		currentPlayer = (currentPlayer%2)+1;
	}
		
	public void mousePressed(MouseEvent e){}
	public void mouseReleased(MouseEvent e){}
	public void mouseEntered(MouseEvent e){}
	public void mouseExited(MouseEvent e){}		
	public void mouseClicked(MouseEvent e)
	{
		if(isRunning==true && isPaused==false)
		{
			if(redCount == 1 && blackCount == 1)
			{
				JOptionPane.showMessageDialog(null,"Please beat the Connect Four AI!","Connect Four", JOptionPane.INFORMATION_MESSAGE);
			}
			JLabel cell = (JLabel)e.getComponent();
			winner=board.getWinner();
			if(winner==-1)
			{
				if(currentPlayer == 1)
				{
					int col = getCol(cell);
					int row = board.dropPiece(col);
					System.out.println(row + ", " + col);
					if(row!=-1)
					{
						drawPiece(col,row);
					}
				}
				else if(currentPlayer == 2)
				{					
					int col = board.findEmptyCol();
					int row = board.dropPiece(col);
					System.out.println(row + ", " + col);
					if(row!=-1)
					{
						drawPiece(col,row);
					}
				}
			}
			else//winner is 1 or 2
			{
				//piece.setIcon(new ImageIcon("src/gameMiniGames/ConnectFour/gray.png"));
				JOptionPane.showMessageDialog(new JPanel(),new String("Player "+winner+" has won!"));
				if(winner == 2)
				{
					restartGame();
				}
				else
				{
					this.removeAll();
				}
			}			
		}
	}
	
	public void clearBoard()
	{
		for(int row=0;row<6;row++)
		{
			for(int col=0;col<7;col++)
			{
				board.clearBoard();
				pos[col][row].removeMouseListener(this);
				pos[col][row].setIcon(new ImageIcon("src/gameMiniGames/ConnectFour/gray.png"));				
			}
		}
	}
	
	public void startGame()
	{
		isRunning=true;
		isPaused=false;
		for(int row=0;row<6;row++)
		{
			for(int col=0;col<7;col++)
			{
				pos[col][row].addMouseListener(this);				
			}
		}
	}
	
	public void restartGame()
	{
		clearBoard();
		startGame();
		
		//piece.setIcon(new ImageIcon("src/gameMiniGames/ConnectFour/red.png"));
		//redMoves.setText("Number of Moves: 0");
		//blackMoves.setText("Number of Moves: 0");
		redCount=1;
		blackCount=1;
	}
}
